#!/bin/bash
set -ex
#set -e
# -------------------------------------------------------------------------------
# Filename:     deploy_env_s.sh
# UpdateDate:   2025/07/18
# Description:  一键搭建 MuseTalk 验证环境。
# Example:      cd /home/share/pytorch2.5/musetalk/tools/ && bash deploy_env_s.sh
# Depends:      commit id： 9deb9bea0d46b085d171d9a417671f6ef1af2f69
# Notes:        本脚本适用于初次进入docker容器环境；不适用于再次进入容器，可能会有意想不到问题
# -------------------------------------------------------------------------------

# 1. 使用 Cambricon Pytorch docker 镜像并激活“pytorch_infer”虚拟环境.
source /torch/venv3/pytorch_infer/bin/activate
# 2.下载源码
cd /workspace/ && git clone https://github.com/TMElyralab/MuseTalk.git
cd /workspace/MuseTalk && git checkout 9deb9be
# 3.安装依赖项（以下根据requirements.txt结合官方docker镜像已有的组件，筛选出独立安装的依赖库）
#pip install -r requirements.txt
cd /workspace/diffusers && bash test.sh && cd /workspace/MuseTalk
pip install /home/share/pytorch2.5/musetalk/dependent_files/tensorflow-2.12.0-cp310-cp310-manylinux2014_x86_64.whl
pip install tensorboard
pip install huggingface_hub
pip install gradio
pip install gdown
pip install omegaconf
pip install ffmpeg-python
#pip install moviepy #推荐用 1.0.3 版本，是社区验证最稳定的版本，兼容性最好。
pip install "moviepy==1.0.3" -i https://pypi.tuna.tsinghua.edu.cn/simple
# 4.安装 MMLab 生态系统包：
pip install --no-cache-dir -U openmim
mim install mmengine
mim install "mmcv==2.2.0"       #mim install mmcv
mim install "mmdet==3.2.0"      #mim install mmdet
mim install "mmpose==1.3.2"     #mim install mmpose
# 5.源码编译安装 ffmpeg x264，否则没有动态库和头文件，使用ffmpeg时会报不支持 libx264 的选项（如 -crf）
cd /workspace/ffmpeg-mlu-v4.2.0/
#apt-get install -y x264
git clone https://code.videolan.org/videolan/x264.git
#git clone --depth 1 https://code.videolan.org/videolan/x264.git
cd /workspace/ffmpeg-mlu-v4.2.0/x264
./configure --enable-shared --enable-static --prefix=/usr/local/neuware --disable-asm
make && make install
cd /usr/local/neuware/lib && cp -rvf libx264* pkgconfig /usr/local/neuware/lib64/
cp -rvf /home/share/pytorch2.5/musetalk/dependent_files/build_ffmpeg.sh /workspace/ffmpeg-mlu-v4.2.0/
cd /workspace/ffmpeg-mlu-v4.2.0/ && bash ./build_ffmpeg.sh
# 6.下载权重(保持联网状态)
#cd /workspace/MuseTalk && bash download_weights.sh
cp -rvf /home/share/pytorch2.5/musetalk/dependent_files/models /workspace/MuseTalk/
#Downloading: "https://www.adrianbulat.com/downloads/python-fan/s3fd-619a316812.pth" to /root/.cache/torch/hub/checkpoints/s3fd-619a316812.pth
mkdir -p ~/.cache/torch/hub/checkpoints/
#wget https://www.adrianbulat.com/downloads/python-fan/s3fd-619a316812.pth -P ~/.cache/torch/hub/checkpoints/
cp -rvf /workspace/MuseTalk/models/s3fd-619a316812.pth ~/.cache/torch/hub/checkpoints/
# 7.验证
#cd /workspace/MuseTalk
#Normal Inference
#bash inference.sh v1.5 normal
#Real-time Inference
#bash inference.sh v1.5 realtime
#Gradio 演示
#python app.py --use_float16 --ffmpeg_path /workspace/ffmpeg-mlu-v4.2.0/install/bin/
